package thread;

public class SyncDemo1 {
    private static boolean success1 = false;//表示是否取款成功
    private static boolean success2 = false;//表示是否取款成功

    public static void main(String[] args) {
        Bank bank = new Bank();
        int sum = 0;
        while (true) {
            sum++;
            Thread t1 = new Thread("张三 ") {
                public void run() {
                    success1 = bank.getMoney(20000);
                }
            };
            Thread t2 = new Thread("李四 ") {
                public void run() {
                    success2 = bank.getMoney(20000);
                }
            };

            t1.start();
            t2.start();
            try {
                Thread.sleep(5);
            } catch (InterruptedException e) {
            }

            if (success1 && success2) {
                System.out.println("成功取款,吃公家饭");
                System.out.println("从共尝试了:"+sum+"次");
                break;
            } else {
                System.out.println("取款失败,");
                bank.saveAccount(20000);//重新存上
            }

        }
    }
}
